Public Class frmMain
    Public counter As Integer = 0
    Public iTunesLink As String
    Public songHookLink As String
    Public myLastFMclient As New LastFM
    'Public nextSongEnd As Date
    'Public lastCheck As Date = Now().AddDays(-1)
    Public oldSongTitle As String = ""
    Public oldSongArtist As String = ""
    Public oldSongTimestamp As Date
    Public oldSongLength As Integer
    Public preMSNStr As String
    ''''''''''''''''''''''''''''''
    '' Change this to your local Nova station
    Public NOVAID As Integer = 100
    '' 100 = Nova 100.3 Melbourne
    '' 919 = Nova 91.9 Adelaide
    '' 937 = Nova 93.7 Perth
    '' 969 = Nova 96.9 Sydney
    '' 1069 = Nova 106.9 Brisbane
    ''''''''''''''''''''''''''''''
    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        Try
            ' Open an XML file.
            Dim reader As New System.Xml.XmlTextReader("http://nowplaying.dmgradio.com.au/NowPlayingData/NOVA" & NOVAID & ".xml")
            'Reset the link each time
            iTunesLink = ""
            While reader.Read()
                Select Case reader.Name
                    Case "SearchResult"
                        If Not reader.IsStartElement Then Exit While

                        Dim startDate = reader.GetAttribute("StartTime")
                        'txtEndTime.Text = reader.GetAttribute("EndTime")
                        If IsDate(startDate) Then
                            oldSongTimestamp = Date.Parse(startDate).ToUniversalTime
                            Debug.Print("Date passed successfully: " & oldSongTimestamp.ToString)
                        Else
                            oldSongTimestamp = Date.UtcNow
                            Debug.Print("Date parsing failed. Using now(). String was: " & startDate)
                        End If
                    Case "CustomProperty"
                        Select Case reader.GetAttribute("PropertyName")
                            Case "iTunesLink"
                                iTunesLink = reader.GetAttribute("PropertyValue")
                            Case "SongHookLink"
                                songHookLink = reader.GetAttribute("PropertyValue")
                        End Select
                    Case "MusicData"
                        ' Get & Set the textboxes
                        txtTitle.Text = Microsoft.VisualBasic.Strings.StrConv(reader.GetAttribute("Title"), VbStrConv.ProperCase, 0)
                        txtArtist.Text = Microsoft.VisualBasic.Strings.StrConv(reader.GetAttribute("Artist"), VbStrConv.ProperCase, 0)

                        ' If the song has changed, scrobble the song to last.fm
                        If myLastFMclient.IsReady() = True And oldSongTitle <> txtTitle.Text And oldSongArtist <> txtArtist.Text And oldSongTitle.Length > 1 Then
                            If oldSongLength > 30 Then
                                myLastFMclient.Scrobble(oldSongTitle, oldSongArtist, Int(oldSongTimestamp.Subtract(#1/1/1970#).TotalSeconds), "R", , , oldSongLength)
                            Else
                                myLastFMclient.Scrobble(oldSongTitle, oldSongArtist, Int(oldSongTimestamp.Subtract(#1/1/1970#).TotalSeconds), "R")
                            End If
                        End If

                        ' Output Now Playing data to exernal services (MSN, Last.fm)
                        MSNStatusMessage.SendStatusMessage(True, MSNStatusMessage.EnumCategory.Music, preMSNStr & txtTitle.Text & " - " & txtArtist.Text)
                        If myLastFMclient.IsReady() Then myLastFMclient.NowPlaying(txtTitle.Text, txtArtist.Text)

                        ' Save old song data
                        oldSongTitle = txtTitle.Text
                        oldSongArtist = txtArtist.Text
                        oldSongLength = Val(reader.GetAttribute("LengthSeconds"))
                End Select
            End While

            ' Show the iTunesLink only if a link was supplied for this song
            If iTunesLink = "" Then lbliTunes.Visible = False Else lbliTunes.Visible = True
            If songHookLink = "" Then lblPlaySample.Visible = False Else lblPlaySample.Visible = True

            'lastCheck = Now()

        Catch ex As System.Net.WebException ' Catch any exceptions generated to prevent the program freezing
            txtTitle.Text = "Error"
            txtArtist.Text = "Error"
        End Try
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        MSNStatusMessage.SendStatusMessage(False, MSNStatusMessage.EnumCategory.Music)
        End
    End Sub

    Private Sub chkAutoRefresh_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAutoRefresh.CheckedChanged
        timRefresh.Enabled = chkAutoRefresh.Checked
    End Sub

    Private Sub timRefresh_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timRefresh.Tick
        counter = counter + 1
        If Val(txtRefresh.Text) < 5 Then txtRefresh.Text = "5"
        If counter >= Val(txtRefresh.Text) Then
            btnCheck_Click(sender, e)
            counter = 0
        End If

        'If Date.Compare(Now(), nextSongEnd) = 1 And Date.Compare(lastCheck, nextSongEnd) = 1 Then
        'Button1_Click(sender, e)
        'counter = 0
        'End If

    End Sub

    Private Sub lbliTunes_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lbliTunes.LinkClicked
        System.Diagnostics.Process.Start(iTunesLink)
    End Sub

    Private Sub btnSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSettings.Click
        frmLastFMLogin.ShowDialog()
    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        frmLastFMLogin.frmLastFMLogin_Load(sender, e)
        frmLastFMLogin.OK_Click(sender, e)
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picNova.Click
        System.Diagnostics.Process.Start("http://www.nova" & NOVAID & ".com.au")
    End Sub

    Private Sub lblPlaySample_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lblPlaySample.LinkClicked
        frmPlayer.AudioURL = "http://audio.dmgradio.com.au/hooks/" & songHookLink
        frmPlayer.Show()
    End Sub
End Class
